This Jupyter notebook aims at showing some basic plotting function of Telma.
Plotting functions are located in OneLife or MultiplesLives classes.
Besides the "classic static" manner of plotting in life actuarial science. Telma permits plotting charts interactively.
import pandas as pd ## Import the data
from telma import OneLife, MultipleLives ## Import OneLife and MultipleLives
Note: this database refers to the G20 countries and that was downloaded from kaggle. Click the following link to obtain it. Dataset lifeTables
file = r'C:\Users\Josè Valencia\Desktop\Actuarial2\lifeTable.csv'
dataset = pd.read_csv(file, index_col=0)
dataset
| Australia_Male | Austria_Male | Belgium_Male | Bulgaria_Male | Canada_Male | Czech Republic_Male | Denmark_Male | Estonia_Male | Finland_Male | France_Male | ... | Netherlands_Female | Poland_Female | Portugal_Female | Russia_Female | Slovakia_Female | Slovenia_Female | Spain_Female | Sweden_Female | United Kingdom_Female | United States_Female | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| age | |||||||||||||||||||||
| 0 | 100000 | 100000 | 100000 | 100000 | 100000 | 100000 | 100000 | 100000 | 100000 | 100000 | ... | 100000 | 100000 | 100000 | 100000 | 100000 | 100000 | 100000 | 100000 | 100000 | 100000 |
| 1 | 99528 | 99600 | 99620 | 98938 | 99488 | 99698 | 99689 | 99582 | 99740 | 99586 | ... | 99649 | 99493 | 99693 | 99279 | 99526 | 99747 | 99713 | 99759 | 99583 | 99415 |
| 2 | 99495 | 99572 | 99570 | 98887 | 99453 | 99674 | 99663 | 99521 | 99723 | 99554 | ... | 99624 | 99461 | 99657 | 99205 | 99486 | 99727 | 99682 | 99735 | 99558 | 99376 |
| 3 | 99471 | 99545 | 99549 | 98854 | 99433 | 99646 | 99657 | 99521 | 99717 | 99531 | ... | 99609 | 99443 | 99639 | 99160 | 99444 | 99717 | 99669 | 99718 | 99538 | 99351 |
| 4 | 99458 | 99528 | 99528 | 98795 | 99421 | 99637 | 99654 | 99521 | 99707 | 99513 | ... | 99595 | 99424 | 99626 | 99130 | 99414 | 99706 | 99657 | 99700 | 99522 | 99332 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 106 | 45 | 20 | 13 | 5 | 48 | 4 | 14 | 15 | 18 | 35 | ... | 75 | 38 | 100 | 14 | 22 | 66 | 134 | 69 | 137 | 198 |
| 107 | 23 | 10 | 6 | 3 | 25 | 2 | 7 | 9 | 9 | 18 | ... | 38 | 19 | 54 | 7 | 11 | 34 | 70 | 34 | 75 | 112 |
| 108 | 12 | 5 | 3 | 1 | 13 | 1 | 3 | 5 | 4 | 9 | ... | 18 | 10 | 28 | 3 | 6 | 17 | 35 | 16 | 39 | 61 |
| 109 | 6 | 2 | 1 | 1 | 6 | 0 | 2 | 2 | 2 | 4 | ... | 9 | 5 | 14 | 2 | 3 | 8 | 17 | 8 | 20 | 32 |
| 110 | 3 | 1 | 1 | 0 | 3 | 0 | 1 | 1 | 1 | 2 | ... | 4 | 2 | 7 | 1 | 1 | 4 | 8 | 3 | 10 | 16 |
111 rows × 56 columns
usa_male = dataset['United States_Male']
usa_female = dataset['United States_Female']
It will be possible to graph either from a cohort function or several.
Example with one cohort: Usa_Male.
Invoking OneLife, lifeplot method has as parameters:
usa_male
age
0 100000
1 99284
2 99237
3 99206
4 99182
...
106 51
107 27
108 14
109 7
110 3
Name: United States_Male, Length: 111, dtype: int64
OneLife().lifeplot(usa_male) ## By default. lx and dx charts will be plotted.
OneLife().lifeplot(usa_female, qx_log=True) ## If qx_log=True (and interactively=False), log(q(x)) will be plotted.
Example: usa_male, usa_female and a "model".
$Clearly:$
\begin{equation} 1- S(X) = F(X)= \begin{cases} 0, & \text{if}\ x < 0 \\ 1 - e^{-0.0001x^{e^{x/115}}}, & \text{if}\ 0 \le X < 115\\ 1, & \text{if}\ x \geq\ 115 \end{cases} \end{equation} $F(115) = 1$ if $1 - e^{(-0.0001 \cdot 115^{e^{(115/115)}})}.$ Consequently: $S(115) = 0$.
import numpy as np
def vettore(): ## The "ficticious model"
x = np.arange(0,115)
numera = np.exp(-.0001*x**(np.exp(x/115)))
l = pd.Series(numera*10**5)
vettore = l[0:-8].round()
return vettore
vettore()
0 100000.0
1 99990.0
2 99980.0
3 99969.0
4 99958.0
...
102 54.0
103 20.0
104 7.0
105 2.0
106 0.0
Length: 107, dtype: float64
## length of the three cohorts
len(usa_male), len(usa_female), len(vettore())
(111, 111, 107)
## Graph of usa_male, usa_female and the dummy model with different length lx
MultipleLives().lifeplot_2(cohort1=usa_male, cohorte2=usa_female, model=vettore())
MultipleLives().lifeplot_2(cohort1=usa_male, model=vettore(),
cohorte2 = usa_female, qx_log=True)
Passing the mouse on it you can observe onto the graph:
france = dataset.France_Female
france.head()# vector lx de las mujeres francesas
age 0 100000 1 99664 2 99638 3 99619 4 99607 Name: France_Female, dtype: int64
MultipleLives().lifeplot_2(usa_male, usa_female, france,
interactive=True) # The parameter "interactive = True" is required.
MultipleLives().lifeplot_2(usa_male, model=vettore(), cohorte2 = usa_female, interactive=True, death=True, qx_log= True)
## Notice that the parameters death = True and qx_log = True are passed.
## "Value" in the graph is the qx of the observed cohort in question.
## While Death indicates the deaths at the respective ages that occur in all 3 cohorts. That is dx
MultipleLives().lifeplot_2(usa_male, model=vettore(), cohorte2 = usa_female, interactive=True, death=True, qx_log= False)
## This time values are the deaths of the cohorts in question. And the probabilities qx are observed